/* global AFRAME, THREE */
AFRAME.registerComponent('gun', {
  schema: {
    bulletTemplate: { default: '#bullet-template' },
    triggerKeyCode: { default: ' ' } // spacebar
  },

  init: function () {
    const self = this;
    self.sceneEl = self.el.sceneEl;
    document.addEventListener('keyup', function (e) {
      if (e.key == self.data.triggerKeyCode) {
        console.log('shoot');
        self.shoot();
      }
    })
  },

  shoot: function () {
    const el = this.createBullet();
    addElementToScene(this.sceneEl, el);
  },

  createBullet: function () {
    const el = document.createElement('a-entity');
    el.setAttribute('networked', 'template:' + this.data.bulletTemplate);
    // el.setAttribute('remove-in-seconds', 10);
    el.setAttribute('forward', 'speed:1');
    // el.setAttribute('dynamic-body', {
    //   shape: 'sphere',
    //   sphereRadius:0.2
    // });
    el.addEventListener('collide', this.shootCollided);

    const tip = document.querySelector('#player');
    el.setAttribute('position', this.getInitialBulletPosition(tip));
    el.setAttribute('rotation', this.getInitialBulletRotation(tip));
    return el;
  },
  // 碰撞探测(aframe-physics-system)
  shootCollided: function (event) {
    // Other entity, which playerEl touched.此处为被子弹射中的物体(气球等)
    const bodyEl = event.detail.body.el;
    // Original entity (playerEl).此处为子弹
    const targetEl = event.detail.target.el;
    // console.log('bodyEl', bodyEl);
    // console.log('targetEl',targetEl);

    console.log(' bullet collide detected');
    if (bodyEl.id === 'floor') {
      console.log('Hit the floor');
      targetEl.removeEventListener('collide', this.shootCollided);
      // targetEl.removeEventListener('destroy', targetEl.destroy);
      targetEl.parentNode.removeChild(targetEl);
    }
    // 子弹击中气球。移除子弹上事件，移除子弹; 移除气球上事件，移除气球 
    else if (bodyEl.className === 'balloon-gun') {
      console.log('Hit the balloon!');
      console.log('bodyEl', bodyEl);
      // console.log('bodyEl getAttribute random-score', bodyEl.getAttribute('random-score', 'score'));
      // 移除子弹上事件，移除子弹
      targetEl.removeEventListener('collide', this.shootCollided);
      targetEl.parentNode.removeChild(targetEl);
      // 击中气球设置分数
      setScoreBoardUserScore();

      // bodyEl.setAttribute('material', 'opacity', 0);
      bodyEl.parentNode.removeChild(bodyEl);
    }
    // 子弹击中cube。移除子弹上事件，移除子弹;触发cube变色事件
    else if (bodyEl.className === 'battle-button') {
      console.log('Hit the battle-button!');
      // 移除子弹上事件，移除子弹
      targetEl.removeEventListener('collide', this.shootCollided);
      // targetEl.setAttribute('material', 'opacity', 0);
      targetEl.parentNode.removeChild(targetEl);

      setScoreBoardUserInfo();//设置记分板用户id
      const shootGameControllerRes=shootGameController();
      console.error('shootGameControllerRes',shootGameControllerRes);
      if(shootGameControllerRes===true){//说明能开始游戏
        playerStartGame();
      }
      // cube不用移除
      // bodyEl.parentNode.removeChild(bodyEl);
    }
  },

  getInitialBulletPosition: function (spawnerEl) {
    const worldPos = new THREE.Vector3();
    worldPos.setFromMatrixPosition(spawnerEl.object3D.matrixWorld);
    return worldPos;
  },

  getInitialBulletRotation: function (spawnerEl) {
    const worldDirection = new THREE.Vector3();

    spawnerEl.object3D.getWorldDirection(worldDirection);
    worldDirection.multiplyScalar(-1);
    this.vec3RadToDeg(worldDirection);

    return worldDirection;
  },

  vec3RadToDeg: function (rad) {
    rad.set(rad.y * 90, -90 + (-THREE.Math.radToDeg(Math.atan2(rad.z, rad.x))), 0);
  },
});

